﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Util;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 空间参考
    /// </summary>
    public partial class CtlSpatialRef : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlSpatialRef()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 默认的最小X的范围
        /// </summary>
        private const double _xmin = -5123200;
        /// <summary>
        /// 默认的最大X的范围
        /// </summary>
        private const double _xmax = 900714802274.099;
        /// <summary>
        /// 默认的最小Y的范围
        /// </summary>
        private const double _ymin = -5123200;
        /// <summary>
        /// 默认的最大Y的范围
        /// </summary>
        private const double _ymax = 900714802274.099;

        /// <summary>
        /// 获取或设置空间参考文件（*.prj）默认存放的路径
        /// </summary>
        public string DefaultPath { get; set; }

        /// <summary>
        /// 已存在的空间参考信息
        /// </summary>
        private List<ISpatialReference> _Spatials = null;
        /// <summary>
        /// 设置已存在的空间参考信息
        /// </summary>
        public List<ISpatialReference> Spatials
        {
            set
            {
                _Spatials = value;
                AddSpatial();
            }
        }

        /// <summary>
        /// 获取或设置当前设置的空间参考对象
        /// </summary>
        public ISpatialReference SpatialRef { get; set; }

        /// <summary>
        /// 最大的Y值
        /// </summary>
        public double MaxY
        {
            get { return double.Parse(txtMaxY.Text); }
            set { txtMaxY.Text = value.ToString(); }
        }

        /// <summary>
        /// 最大的X值
        /// </summary>
        public double MaxX
        {
            get { return double.Parse(txtMaxX.Text); }
            set { txtMaxX.Text = value.ToString(); }
        }

        /// <summary>
        /// 最小的Y值
        /// </summary>
        public double MinY
        {
            get { return double.Parse(txtMinY.Text); }
            set { txtMinY.Text = value.ToString(); }
        }

        /// <summary>
        /// 最小的X值
        /// </summary>
        public double MinX
        {
            get { return double.Parse(txtMinX.Text); }
            set { txtMinX.Text = value.ToString(); }
        }

        /// <summary>
        /// 打开Pri文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog.Multiselect = false;
            if (!string.IsNullOrWhiteSpace(DefaultPath) && Directory.Exists(DefaultPath))
                openFileDialog.InitialDirectory = DefaultPath;
            if (openFileDialog.ShowDialog(this.ParentForm) != DialogResult.OK) return;
            cmbSptial.Text = openFileDialog.FileName;
            SetSpatial(cmbSptial.Text);
        }

        /// <summary>
        /// 选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSptial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSptial.SelectedItem == null) return;
            SetSpatial(((ValueName2)cmbSptial.SelectedItem).Tag);
        }

        /// <summary>
        /// 设置可选的坐标系信息
        /// </summary>
        private void AddSpatial()
        {
            Clear();
            cmbSptial.DataSource = null;
            List<ValueName2> ds = new List<ValueName2>();
            //ds.Add(new ValueName2() { Name = "", Value = "", Tag = "" });
            if (_Spatials != null && _Spatials.Count > 0)
            {
                foreach (ISpatialReference itm in _Spatials)
                {
                    ds.Add(new ValueName2() { Name = itm.Name, Value = itm.Name, Tag = itm });
                }
            }
            if (!string.IsNullOrWhiteSpace(DefaultPath) && Directory.Exists(DefaultPath))
            {
                string[] fs = Directory.GetFiles(DefaultPath);
                foreach (string s in fs)
                {
                    ds.Add(new ValueName2() { Name = System.IO.Path.GetFileName(s), Value = s, Tag = s });
                }
            }
            cmbSptial.DataSource = ds;
            cmbSptial.ValueMember = "Value";
            cmbSptial.DisplayMember = "Name";
        }

        /// <summary>
        /// 设置所选择的坐标系
        /// </summary>
        /// <param name="value"></param>
        private void SetSpatial(dynamic value)
        {
            Clear();
            if (value is string)
            {
                if (string.IsNullOrWhiteSpace(value as string)) return;
                SpatialRef = GeometryHelper.CreateSpatialReference(value as string);
            }
            else if (value is ISpatialReference)
            {
                SpatialRef = value as ISpatialReference;
            }
            else
            {
                return;
            }
            double xmin = 0;
            double xmax = 0;
            double ymin = 0;
            double ymax = 0;
            SpatialRef.GetDomain(out xmin, out xmax, out ymin, out ymax);
            MinX = xmin;
            MaxX = xmax;
            MinY = ymin;
            MaxY = ymax;
        }

        /// <summary>
        /// 将坐标系信息清空
        /// </summary>
        private void Clear()
        {
            SpatialRef = new UnknownCoordinateSystem() as ISpatialReference;
            MinX = _xmin;
            MaxX = _xmax;
            MinY = _ymin;
            MaxY = _ymax;
        }
    }
}
